package com.itheima.stargraph.mapper;

import com.itheima.stargraph.pojo.UserFund;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * 用户账户信息表 Mapper 接口
 * </p>
 *
 * @author mr.wu
 * @since 2025-06-14
 */
public interface UserFundMapper extends BaseMapper<UserFund> {

    /**
     * 账户积分变更（积分账户扣减、积分冻结账户增加）
     * @param id
     * @param score
     * @return
     */
    @Update("update sg_user_fund set score=score-#{score},freeze_score=freeze_score+#{score} where id=#{id} and score>=#{score}")
    Long updateForFreeze(@Param("id") Long id, @Param("score") Long score);

    /**
     * 账户积分变更（积分账户增加、积分冻结账户扣减）
     * @param id
     * @param score
     * @return
     */
    @Update("update sg_user_fund set score=score+#{score},freeze_score=freeze_score-#{score} where id=#{id} and freeze_score>=#{score}")
    Long updateForRefund(@Param("id") Long id, @Param("score") Long score);

    /**
     * 积分冻结账户扣减
     * @param id
     * @param score
     * @return
     */
    @Update("update sg_user_fund set freeze_score=freeze_score-#{score} where id=#{id} and freeze_score>=#{score}")
    Long updateFreeScoreDecrement(@Param("id") Long id, @Param("score") Long score);

    /**
     * 总账户积分账户增加
     * @param score
     * @return
     */
    @Update("update sg_user_fund set score=score+#{score} where id=0")
    Long updateTotalScoreIncrement( @Param("score") Long score);

    /**
     * 积分账户扣减
     * @param id
     * @param score
     * @return
     */
    @Update("update sg_user_fund set score=score-#{score} where id=#{id} and score>=#{score}")
    Long updateScoreDecrement(@Param("id") Long id, @Param("score") Long score);
}
